<?php

require_once('config.php');
require_once('mydns.function.php');
require_once('mydns.class.php');
require_once('webcolor.function.php');
require_once('subnetMatcher.php');

$net = new subnetMatcher($__ACCESS_ALLOWED_IP);
if(!$net->match($REMOTE_ADDR)) {
	header('HTTP/1.0 403 Access Denied');
	die("<h1>Access Denied</h1>");
}
// background color
$bgcolor	= '#B5C1C7'; //'#ddddff';
// td background color
$tdtint		= '#ffffff';
$tdbg		= webcolor_blend($bgcolor, $tdtint, 0.3);
// edited input color
$editcolor	= '#ffffcc';
// warn input color
$warncolor	= '#ffe0e0';

session_start();

if(is_string($_POST['new'])) {
	$newdomain =& new soa($_POST['newdomain'], null, $__DEFAULT_NEWDOMAIN_RECORD);
	// Template resource records
	if(is_array($__DEFAULT_NEWDOMAIN_RR)) {
		foreach($__DEFAULT_NEWDOMAIN_RR as $r) {
			$newdomain->addRr(new rr($r));
		}
	}
}

if(is_string($_POST['save']) || is_string($_POST['saveReverse'])) {
	//echo "<pre>"; var_dump($_POST['soa']); var_dump($_POST['rr']); echo "</pre>";
	//Save changes to the database!

	$domain =& new soa($_POST['domain']);

	$domain->setOrigin ($_POST['soa']['origin']);
	$domain->setNs     ($_POST['soa']['ns']);
	$domain->setMbox   ($_POST['soa']['mbox']);
	$domain->setRefresh($_POST['soa']['refresh']);
	$domain->setRetry  ($_POST['soa']['retry']);
	$domain->setExpire ($_POST['soa']['expire']);
	$domain->setMinimum($_POST['soa']['minimum']);
	$domain->setTtl    ($_POST['soa']['ttl']);
	if(!is_string($_POST['saveReverse'])) {
/*		$rrs = array();
		if(is_array($_POST['rr'])) {
			foreach($_POST['rr'] as $r) {
				$rrs[] =& new rr($r);
			}
		}
		$domain->setRr($rrs); */
		$rrs =& $domain->getRr();
		for($c = 0; isset($_POST['rr'][$c]); $c++) {
			if(!isset($rrs[$c])) $rrs[$c] =& new rr(array('id'=>0));
			$rr =& $rrs[$c];
			$rr->setName($_POST['rr'][$c]['name']);
			$rr->setType($_POST['rr'][$c]['type']);
			$rr->setData($_POST['rr'][$c]['data']);
			$rr->setAux ($_POST['rr'][$c]['aux']);
			$rr->setTtl ($_POST['rr'][$c]['ttl']);
		}
	} else {
		$domain->deleteAllRr();
		$domain->setRr(soaFactory::getReverseRrCollectionByIp($domain->isReverse()));
	}
	
	$domain->commit();
	$_SESSION['d'] = $_POST['soa']['origin'];
}


if(isset($_GET['del_rr'])) {
	if(isset($_SESSION['d'])) {
		$domain = new soa($_SESSION['d']);
		$domain->deleteRr($_GET['del_rr']);
	}
}

if(isset($_GET['del_soa'])) {
	$domain = new soa($_GET['del_soa']);
	$domain->deleteYourself();
}

if(is_string($_GET['d'])) {
	$_SESSION['d'] = $_GET['d'];
	header('Location: .');
	die();
}

if($_GET['reset'] || $_POST['reset']) {
	session_destroy();
	unset($_SESSION);
}

echo "<html>\n <head>\n  <title>MyDNS</title>\n";
echo "  <script type=\"text/javascript\">
	function getEditColor() {
		return '$editcolor';
	}
	function getWarnColor() {
		return '$warncolor';
	}
	function validate_data_text(obj) {
		if(
			obj.value.match(/^((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$/) ||
			obj.value.match(/^(([a-zA-Z0-9\-]+\.)+)$/)
		) {
			obj.style.backgroundColor = getEditColor();
		} else {
			obj.style.backgroundColor = getWarnColor();
		}
	}
	function validate_name_text(obj) {
		if(
			obj.value.match(/^([\*]|[a-zA-Z0-9\-\.]+)$/)
		) {
			obj.style.backgroundColor = getEditColor();
		} else {
			obj.style.backgroundColor = getWarnColor();
		}
	}
	function validate_int_text(obj) {
		if(
			obj.value.match(/^[0-9]+$/)
		) {
			obj.style.backgroundColor = getEditColor();
		} else {
			obj.style.backgroundColor = getWarnColor();
		}
	}
  </script>
  <style>
	body {
		font-family: sans-serif;
		background: $bgcolor;
	}
	img {
		border: none;
	}
	th {
		text-align: left;
	}
	th.num {
		text-align:	right;
	}
	td {
		font-family: monospace;
		border: none;
		background: $tdbg;
	}
	
	td.num {
		text-align: right
	}

	input {
		border: solid 1px #aaaaaa;
	}
	
	input.num {
		text-align: right
	}
  </style>\n";
echo " </head>\n <body>";

//echo "<pre>"; var_dump($_SESSION); echo "</pre>";

if(is_string($_SESSION['d']) || isset($newdomain)) {

	if(isset($newdomain)) {
		$domain =& $newdomain;
	} else {
		$domain = new soa($_SESSION['d']);
	}

	if(is_string($_POST['newrr'])) {
		$rr =& $domain->getRr();
		$rr[] =& new rr(array('id' => 0, 'aux' => 0, 'ttl' => 86400));
	}


	
	if(is_string($_POST['reviewReverse']) and $like = $domain->isReverse()) {
		$reviewReverse = true;
		// replace the domain's rr set with the automatic one
		$domain->setRr(soaFactory::getReverseRrCollectionByIp($like));
	} else {
		$reviewReverse = false;
	}
	
	echo "<form method=\"POST\" action=\".\">";
	if($domain->getOrigin()) {
		$heading = $reviewReverse ? $domain->getOrigin()." (AutoUpdate review)" : $domain->getOrigin();
	} else {
		$heading = "[empty]";
	}
	printf("<h1>%s</h1>", $heading);
	printf("<input type=\"hidden\" name=\"domain\" value=\"%s\">", $domain->getOrigin());
	
	echo "<table width=\"100%\">";
	echo "<tr>";
	echo "<th>Origin</th>";
	echo "<th>NS</th>";
	echo "<th>Mbox</th>";
	echo "<th class=\"num\">Serial</th>";
	echo "<th class=\"num\">Refresh</th>";
	echo "<th class=\"num\">Retry</th>";
	echo "<th class=\"num\">Expire</th>";
	echo "<th class=\"num\">Minimum</th>";
	echo "<th class=\"num\">TTL</th>";
	echo "</tr>";
	echo "<tr>";
	printf("<td>%s</td>", soa_origin('soa[origin]', $domain->getOrigin()));
	printf("<td>%s</td>", soa_ns('soa[ns]', $domain->getNs()));
	printf("<td>%s</td>", soa_mbox('soa[mbox]', $domain->getMbox()));
	printf("<td class=\"num\">%s</td>", soa_serial('soa[serial]', $domain->getSerial()));
	printf("<td class=\"num\">%s</td>", soa_refresh('soa[refresh]', $domain->getRefresh()));
	printf("<td class=\"num\">%s</td>", soa_retry('soa[retry]', $domain->getRetry()));
	printf("<td class=\"num\">%s</td>", soa_expire('soa[expire]', $domain->getExpire()));
	printf("<td class=\"num\">%s</td>", soa_minimum('soa[minimum]', $domain->getMinimum()));
	printf("<td class=\"num\">%s</td>", soa_ttl('soa[ttl]', $domain->getTtl()));
	echo "</tr>";
	echo "</table>";

	echo "<table width=\"100%\">";
	echo "<tr>";
	echo "<th>Name</th>";
	echo "<th>Type</th>";
	echo "<th>Data</th>";
	echo "<th class=\"num\">Pri</th>";
	echo "<th class=\"num\">TTL</th>";
	echo "<th></th>";
	echo "</tr>";
	foreach($domain->getRr() as $k=>$v) {
		echo "<tr>";
		printf("<td>%s</td>", rr_name("rr[$k][name]", $v->getName()));
		printf("<td>%s</td>", rr_type("rr[$k][type]", $v->getType()));
		printf("<td>%s</td>", rr_data("rr[$k][data]", $v->getData()));
		printf("<td class=\"num\">%s</td>", rr_aux("rr[$k][aux]", $v->getAux()));
		printf("<td class=\"num\">%s</td>", rr_ttl("rr[$k][ttl]", $v->getTtl()));
		printf("<td class=\"num\">%s</td>", $v->getId() ? button_delete($SELF."?del_rr=".$k) : '');
		echo "</tr>";
	}
	echo "</table>";
	if(!$reviewReverse) {
		if(!isset($newdomain)) echo "<input type=\"submit\" name=\"newrr\" value=\"New\"> ";
		echo "<input type=\"submit\" name=\"save\" value=\"Save\" onclick=\"return confirm('Save new domain information and update serial?');\"> ";
		echo "<input type=\"submit\" name=\"reset\" value=\"Return\"> ";
	
		if($domain->isReverse()) {
			echo "<input type=\"submit\" name=\"reviewReverse\" value=\"AutoUpdate reverse zone\"> ";
		}
	} else {
		echo "<input type=\"submit\" name=\"saveReverse\" value=\"Save\" onclick=\"return confirm('This will replace all records in this domain with new ones!');\"> ";
		echo "<input type=\"submit\" name=\"resetReview\" value=\"Cancel\"> ";
	}
	echo "</form>";
	

} else {

	$db = new myDnsSql();
	
	$domains = $db->getColArray('select origin from soa order by id;', 'origin');
	echo "<form method=\"POST\" action=\".\">";
	echo "<table width=\"100%\">";
	echo "<tr>";
	echo "<th>Origin</th>";
	echo "<th></th>";
	echo "<th></th>";
	echo "<th>NS</th>";
	echo "<th>Mbox</th>";
	echo "<th class=\"num\">Serial</th>";
	echo "<th class=\"num\">Refresh</th>";
	echo "<th class=\"num\">Retry</th>";
	echo "<th class=\"num\">Expire</th>";
	echo "<th class=\"num\">Minimum</th>";
	echo "<th class=\"num\">TTL</th>";
	echo "</tr>";
	foreach($domains as $domain) {
		$domain = new soa($domain);
		//printf("<a href=\"%s\">%s</a><br />\n", $SELF."?d=".rawurlencode($domain), $domain);
		echo "<tr>";
		printf("<td>%s</td>", $domain->getOrigin());
		printf("<td>%s</td>", button_edit($SELF."?d=".rawurlencode($domain->getOrigin())));
		printf("<td>%s</td>", button_delete($SELF."?del_soa=".rawurlencode($domain->getOrigin())));
		printf("<td>%s</td>", $domain->getNs());
		printf("<td>%s</td>", $domain->getMbox());
		printf("<td class=\"num\">%s</td>", $domain->getSerial());
		printf("<td class=\"num\">%s</td>", $domain->getRefresh());
		printf("<td class=\"num\">%s</td>", $domain->getRetry());
		printf("<td class=\"num\">%s</td>", $domain->getExpire());
		printf("<td class=\"num\">%s</td>", $domain->getMinimum());
		printf("<td class=\"num\">%s</td>", $domain->getTtl());
		echo "</tr>";
	}
	echo "</table>";
	echo "<input type=\"submit\" name=\"new\" value=\"New domain\"> ";
	echo "</form>";
}

echo "</body></html>";

session_write_close();
